<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API调试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        .result {
            margin: 10px 0;
            padding: 10px;
            border-radius: 5px;
            white-space: pre-wrap;
            font-family: monospace;
        }
        .success { background-color: #d4edda; }
        .error { background-color: #f8d7da; }
        .loading { background-color: #fff3cd; }
        button {
            padding: 10px 20px;
            margin: 5px;
            border: none;
            border-radius: 5px;
            background-color: #007bff;
            color: white;
            cursor: pointer;
        }
        button:hover { background-color: #0056b3; }
    </style>
</head>
<body>
    <h1>API调试工具</h1>
    
    <button onclick="testBasicAPI()">测试基础API</button>
    <button onclick="testRoleplayAPI()">测试角色扮演API</button>
    <button onclick="testWithTimeout()">测试超时设置</button>
    
    <div id="result" class="result" style="display: none;"></div>

    <script>
        function showResult(content, type = 'success') {
            const result = document.getElementById('result');
            result.style.display = 'block';
            result.className = `result ${type}`;
            result.textContent = content;
        }

        async function testBasicAPI() {
            showResult('正在测试基础API...', 'loading');
            
            try {
                const response = await fetch('http://localhost:8080/api/test/deepseek', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        prompt: '你好'
                    })
                });

                const result = `状态码: ${response.status}\n状态文本: ${response.statusText}\n响应头: ${JSON.stringify([...response.headers.entries()], null, 2)}`;
                
                if (response.ok) {
                    const data = await response.json();
                    showResult(result + '\n\n响应数据: ' + JSON.stringify(data, null, 2), 'success');
                } else {
                    const errorText = await response.text();
                    showResult(result + '\n\n错误响应: ' + errorText, 'error');
                }
            } catch (error) {
                showResult('请求失败: ' + error.message, 'error');
            }
        }

        async function testRoleplayAPI() {
            showResult('正在测试角色扮演API...', 'loading');
            
            try {
                const response = await fetch('http://localhost:8080/api/test/roleplay', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        character: '苏格拉底',
                        prompt: '你好'
                    })
                });

                const result = `状态码: ${response.status}\n状态文本: ${response.statusText}\n响应头: ${JSON.stringify([...response.headers.entries()], null, 2)}`;
                
                if (response.ok) {
                    const data = await response.json();
                    showResult(result + '\n\n响应数据: ' + JSON.stringify(data, null, 2), 'success');
                } else {
                    const errorText = await response.text();
                    showResult(result + '\n\n错误响应: ' + errorText, 'error');
                }
            } catch (error) {
                showResult('请求失败: ' + error.message, 'error');
            }
        }

        async function testWithTimeout() {
            showResult('正在测试超时设置...', 'loading');
            
            try {
                const controller = new AbortController();
                const timeoutId = setTimeout(() => controller.abort(), 10000); // 10秒超时

                const response = await fetch('http://localhost:8080/api/test/roleplay', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        character: '苏格拉底',
                        prompt: '你好'
                    }),
                    signal: controller.signal
                });

                clearTimeout(timeoutId);

                const result = `状态码: ${response.status}\n状态文本: ${response.statusText}`;
                
                if (response.ok) {
                    const data = await response.json();
                    showResult(result + '\n\n响应数据: ' + JSON.stringify(data, null, 2), 'success');
                } else {
                    const errorText = await response.text();
                    showResult(result + '\n\n错误响应: ' + errorText, 'error');
                }
            } catch (error) {
                if (error.name === 'AbortError') {
                    showResult('请求超时（10秒）', 'error');
                } else {
                    showResult('请求失败: ' + error.message, 'error');
                }
            }
        }
    </script>
</body>
</html>

